package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.ErrorConstants;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.PmsPatientDto;
import com.ruoyi.system.domain.po.BlsZlInfo;
import com.ruoyi.system.domain.po.BlsZlInfoExample;
import com.ruoyi.system.domain.po.PmsPatient;
import com.ruoyi.system.domain.po.PmsPatientExample;
import com.ruoyi.system.domain.vo.PmsPatientVo;
import com.ruoyi.system.mapper.BlsZlInfoMapper;
import com.ruoyi.system.mapper.PmsPatientMapper;
import com.ruoyi.system.service.PmsPatientService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author nanyang
 */
@Service
public class PmsPatientServiceImpl implements PmsPatientService {
    
    @Resource
    private PmsPatientMapper pmsPatientMapper;
    @Resource
    private BlsZlInfoMapper blsZlInfoMapper;
    
    
    @Override
    public Integer insertPmsPatient(PmsPatientDto.Insert pmsPatientInsert) {
        // 检查身份证号是否重复
        PmsPatientExample pmsPatientExample = new PmsPatientExample();
        pmsPatientExample.createCriteria().andDelFlagEqualTo((byte) 0).andIdCardEqualTo(pmsPatientInsert.getIdCard());
        List<PmsPatient> pmsPatients = pmsPatientMapper.selectByExample(pmsPatientExample);
        if (!pmsPatients.isEmpty()) {
            throw new BaseException(ErrorConstants.DUPLICATE_ID_CARD);
        }
        // 新增实体数据
        PmsPatient pmsPatient = BeanUtil.copyProperties(pmsPatientInsert, PmsPatient.class);
        pmsPatient.setId(IdUtil.getSnowflake().nextId());
        pmsPatient.setCreateBy(pmsPatientInsert.getSourceBy());
        pmsPatient.setCreateTime(pmsPatientInsert.getSourceTime());
        pmsPatient.setCreateCompany(pmsPatientInsert.getSourceCompany());
        return pmsPatientMapper.insertSelective(pmsPatient);
    }

    @Override
    public Integer updatePmsPatient(PmsPatientDto.Update pmsPatientUpdate) {
        // 检查身份证号是否重复
//        if (StrUtil.isNotBlank(pmsPatientUpdate.getIdCard())) {
//            PmsPatientExample pmsPatientExample = new PmsPatientExample();
//            pmsPatientExample.createCriteria().andDelFlagEqualTo((byte) 0).andIdCardEqualTo(pmsPatientUpdate.getIdCard());
//            List<PmsPatient> pmsPatients = pmsPatientMapper.selectByExample(pmsPatientExample);
//            if (!pmsPatients.isEmpty()) {
//                throw new BaseException(ErrorConstants.DUPLICATE_ID_CARD);
//            }
//        }
        // 修改实体
        PmsPatient pmsPatient = BeanUtil.copyProperties(pmsPatientUpdate, PmsPatient.class);
        pmsPatient.setUpdateBy(SecurityUtils.getUsername());
        pmsPatient.setUpdateTime(new Date());
        return pmsPatientMapper.updateByPrimaryKeySelective(pmsPatient);
    }

    @Override
    public Integer deletePmsPatient(PmsPatientDto.Delete pmsPatientDelete) {
        PmsPatientExample interfaceExample = new PmsPatientExample();
        interfaceExample.createCriteria().andIdIn(pmsPatientDelete.getIdList());
        PmsPatient pmsPatient = new PmsPatient();
        pmsPatient.setDelFlag((byte) 2);
        return pmsPatientMapper.updateByExampleSelective(pmsPatient, interfaceExample);
    }

    @Override
    public PageInfo<PmsPatientVo.Result> listPmsPatient(PmsPatientDto.Query queryPmsPatient) {
        PmsPatientExample pmsPatientExample = new PmsPatientExample();
        PmsPatientExample.Criteria criteria = pmsPatientExample.createCriteria().andDelFlagEqualTo((byte) 0);
        if (StrUtil.isNotBlank(queryPmsPatient.getName())) {
            criteria.andNameLike("%" + queryPmsPatient.getName() + "%");
        }
        if (StrUtil.isNotBlank(queryPmsPatient.getPinyinFull())) {
            criteria.andPinyinFullLike("%" + queryPmsPatient.getPinyinFull() + "%");
        }
        if (StrUtil.isNotBlank(queryPmsPatient.getPinyinSimple())) {
            criteria.andPinyinSimpleLike("%" + queryPmsPatient.getPinyinSimple() + "%");
        }
        if (queryPmsPatient.getSex() != null) {
            criteria.andSexEqualTo(queryPmsPatient.getSex());
        }
        if (StrUtil.isNotBlank(queryPmsPatient.getIdCard())) {
            criteria.andIdCardLike("%" + queryPmsPatient.getIdCard() + "%");
        }
        if (queryPmsPatient.getSource() != null) {
            criteria.andSourceEqualTo(queryPmsPatient.getSource());
        }

        if (queryPmsPatient.getSource() != null) {
            criteria.andSourceEqualTo(queryPmsPatient.getSource());
        }
        pmsPatientExample.setOrderByClause("create_time desc");
        List<PmsPatient> pmsPatientList = pmsPatientMapper.selectByExample(pmsPatientExample);
        List<PmsPatientVo.Result> collect = pmsPatientList.stream().map(e -> BeanUtil.copyProperties(e, PmsPatientVo.Result.class)).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(collect)) {
            for (PmsPatientVo.Result result : collect) {
                BlsZlInfoExample blsZlInfoExample = new BlsZlInfoExample();
                blsZlInfoExample.createCriteria().andDelFlagEqualTo((byte) 0).andPatientIdEqualTo(result.getId());
                blsZlInfoExample.setOrderByClause("id desc");
                List<BlsZlInfo> blsZlInfos = blsZlInfoMapper.selectByExample(blsZlInfoExample);
                if (CollectionUtil.isNotEmpty(blsZlInfos)) {
                    result.setDnh(blsZlInfos.get(0).getId());
                }
            }
        }
        PageInfo<PmsPatientVo.Result> resultPageInfo = new PageInfo<>(collect);
        PageInfo<PmsPatient> pmsPatientPageInfo = new PageInfo<>(pmsPatientList);
        resultPageInfo.setTotal(pmsPatientPageInfo.getTotal());
        resultPageInfo.setPageNum(pmsPatientPageInfo.getPageNum());
        resultPageInfo.setStartRow(pmsPatientPageInfo.getStartRow());
        resultPageInfo.setPageSize(pmsPatientPageInfo.getPageSize());
        resultPageInfo.setEndRow(pmsPatientPageInfo.getEndRow());
        return resultPageInfo;
    }

    @Override
    public PmsPatientVo.Result selectPmsPatientByIdCard(PmsPatientDto.QueryByIdCard query) {
        PmsPatientExample pmsPatientExample = new PmsPatientExample();
        pmsPatientExample.createCriteria().andDelFlagEqualTo((byte) 0).andIdCardEqualTo(query.getIdCard());
        List<PmsPatient> pmsPatients = pmsPatientMapper.selectByExample(pmsPatientExample);
        if (pmsPatients.isEmpty()) {
            return null;
        }
        return BeanUtil.copyProperties(pmsPatients.get(0), PmsPatientVo.Result.class);
    }
}
